<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/15
 * Time: 11:44
 */

namespace app\api\controller\v2;

use app\api\lib\exception\MissException;
use app\api\lib\exception\SuccessMessage;
use app\api\validate\IdMustInt;
use app\api\model\Collage as CollageModel;
use app\api\model\Goods as GoodsModel;



class CollageController extends BaseController
{
    //获取拼团的商品列表
    public function getCollage()
    {
        $collages = CollageModel::checkCollage();
        $collageGoodsIds = [];
        foreach($collages as $k=>$v){
            $collageGoodsIds[] = $v->getData('id');
        }
        $collageGoods = CollageModel::with(['collageGoods'=>function($query){
            $query->field('id,goods_name,cover_img,cover_img_origin,sale_price,sales_num,is_recommend,model_id');
        }])->field('id,collage_name,goods_price,collage_price,start_time,end_time,goods_id')->where('id','in',$collageGoodsIds)->select();
        if($collageGoods){
            throw new SuccessMessage(['data'=>$collageGoods]);
        }else{
            throw new MissException(['msg'=>'当前没有拼团的商品']);
        }
    }

    //获取拼团的商品详情
    public function getCollageDetails($id)
    {
        (new IdMustInt())->goCheck();
        $collageGood = CollageModel::with(['collageGoods'=>function($query){
            $query->field('id,goods_name,cover_img,cover_img_origin,sale_price,sales_num,is_recommend,model_id');
        }])->field('id,collage_name,goods_price,collage_price,start_time,end_time,goods_id')
        ->where('id','eq',$id)
        ->find();
        $proof = CollageModel::proofCollage($collageGood->getData('start_time'),$collageGood->getData('end_time'));
        if(!$proof){
            throw new MissException(['msg'=>'拼团已结束,下次早点来哦！']);
        }
        if($collageGood){
            throw new SuccessMessage(['data'=>$collageGood]);
        }else{
            throw new MissException(['msg'=>'服务器内部错误，请稍后再试！']);
        }
    }
}